
//方法1:  定义一个方法, 将路由菜单数组 menuRoutes 转成 treeData结构的数据
interface Iitem {
    path: string,
    label: string,
    children?: Array<Iitem>
}
export function getTreeDataFn(menuRoutes: any) {
    // 定义数组为得到的treedata数组

    const targetArr: Array<Iitem> = [];
    menuRoutes.forEach((item: any, index: number) => {
        let obj: Iitem = { path: '', label: '' };
        if (item.meta) {
            if (item.children) {
                obj.path = item.path
                obj.label = item.meta.title
                obj.children = getTreeDataFn(item.children) // 使用了递归实现
                targetArr.push(obj)
            } else {
                obj.path = item.path
                obj.label = item.meta.title
                targetArr.push(obj)
            }
        }
    });

    return targetArr
}

// menuRoutes数组=> targetArr [{path:'',label:'},{path:'',label:'}]


// 方法2:  将checkedKeys 数组中转成checkedKeys(包含了父级路径)
// 案例:arr= ['/home','/banner','/banner/list','/pro/prolist','/pro/filterlist']=>
// newarr =  ['/home','/banner','/banner/list','/pro','/pro/prolist','/pro/filterlist']

export function getNewCheckedKeysFn(checkedKeys: Array<string>) {
    let newCheckedKeysArr: Array<string> = [];
    checkedKeys.forEach(item => {
        let arr = item.split('/').filter(v => v);  // ['home'], ['banner'],['banner','list'],['pro','prolist']
        // console.log('arr', arr);
        let tempPath = '';
        arr.forEach(item1 => {
            tempPath += '/' + item1  // /home, /banner, '/banner', /banner/list, /pro, /pro/prolist
            // if (!newCheckedKeysArr.includes(tempPath)) { //判断去重
            //     newCheckedKeysArr.push(tempPath)
            // }
            newCheckedKeysArr.push(tempPath)
        })
    })

    return Array.from(new Set(newCheckedKeysArr))
}

//方法3:  定义一个方法, 根据 newCheckedKeysArr 数组 删除menuRoutes中没有的菜单项
// newCheckedKeysArr= ['/home', '/account', '/account/adminlist', '/banner', '/banner/bannerlist', '/pro', '/pro/prolist', '/pro/filterlist', '/setting']
export function getNewMenuRoutesFn(newCheckedKeysArr: Array<string>, menuRoutes: any) {
    for (let i = menuRoutes.length - 1; i >= 0; i--) {
        const route = menuRoutes[i];
        if (!newCheckedKeysArr.includes(route.path)) {
            menuRoutes.splice(i, 1)
        }

        if (route.children) {
            getNewMenuRoutesFn(newCheckedKeysArr, route.children)
        }
    }

    return menuRoutes
}